/*
 * Create Author  : qiufeng.wu
 * Create Date    : 2012-9-7
 * Project        : dp-search-builder
 * File Name      : ProjectConfigManager.java
 *
 * Copyright (c) 2010-2015 by Shanghai HanTao Information Co., Ltd.
 * All rights reserved.
 *
 */
package com.dp.searchbuilder.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 功能描述: 配置文件管理<p>
 * 
 *
 * @author qiufeng.wu
 *
 * @version 2.2 2012-9-7
 *
 * @since dp-search-builder 2.2
 */
public class ProjectConfigManager
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectConfigManager.class);
    
    private String configFile;
    
    private String workspace;
    
    private String uploadFolder;
    
    private Map<String, ProjectConfig> configs = new HashMap<String, ProjectConfig>();
    
    /**
     * setter method
     * @see ProjectConfigManager#configFile
     * @param configFile the configFile to set
     */
    public void setConfigFile(String configFile)
    {
        this.configFile = configFile;
    }
    
    /**
     * getter method 
     * @see ProjectConfigManager#workspace
     * @return the workspace
     */
    public String getWorkspace()
    {
        return workspace;
    }

    /**
     * getter method 
     * @see ProjectConfigManager#uploadFolder
     * @return the uploadFolder
     */
    public String getUploadFolder()
    {
        return uploadFolder;
    }

    public void init()
    {
        try
        {
            XMLConfiguration conf = new XMLConfiguration(configFile);
            conf.load();
            
            workspace = conf.getString("workspace");
            uploadFolder = conf.getString("uploadfolder");
            List<?> projectConfigs = conf.configurationsAt("project");
            Iterator<?> it = projectConfigs.iterator();
            while (it.hasNext())
            {
                HierarchicalConfiguration projectConfig = (HierarchicalConfiguration) it.next();
                String name = projectConfig.getString("name");
                String repository = projectConfig.getString("repository");
                List<String> modules = new ArrayList<String>();
                String[] moduleArray = projectConfig.getStringArray("moudles.module");
                for (String module  : moduleArray)
                {
                    modules.add(module);
                }
                List<String> cmds = new ArrayList<String>();
                String[] cmdArray = projectConfig.getStringArray("cmds.cmd");
                for (String cmd : cmdArray)
                {
                    cmds.add(cmd);
                }
                
                ProjectConfig config = new ProjectConfig();
                config.setName(name);
                config.setRepository(repository);
                config.setModules(modules);
                config.setCmds(cmds);
                
                configs.put(name, config);
            }
        }
        catch (ConfigurationException e)
        {
            LOGGER.error("invalid config", e);
        }
    }

    public void addConfiguration(ProjectConfig config)
    {
        if (config == null)
        {
            LOGGER.error("invalid config");
            
            return;
        }
        
        configs.put(config.getName(), config);
    }
    
    public ProjectConfig getConfiguration(String name)
    {
        return configs.get(name);
    }
}
